package Datebase;

import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import java.awt.*;
import java.net.URL;
import java.sql.*;

public class term1 {


    public static void main(String[] args) {
        System.setProperty("sun.java2d.noddraw", "true");
        new term1("test","test");
    }
    public term1(String term,String sno){

        JFrame student = new JFrame("学期成绩");
        URL resource = term1.class.getClassLoader().getResource("kun.png");
        Image image = new ImageIcon(resource).getImage();
        student.setIconImage(image);
        //窗口尺寸
        student.setSize(800, 600);
        student.setLocationRelativeTo(null);

        student.setLayout(null);


        //JButton button1 = new JButton("2022秋季学期");
        //button1.addActionListener(new ActionListener() {

        JPanel panel = new JPanel();
        //panel.add(button1);

        student.add(panel);
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            String dbURL = "jdbc:oracle:thin:@localhost:1521:XE";
            String user = "HR";
            String password = "root";
            Connection conn = DriverManager.getConnection(dbURL, user, password);
            String sql = "SELECT CNAME,GRADE,TERM FROM COURSE,EXAM WHERE COURSE.CNO=EXAM.CNO AND COURSE.TERM = '"+term+"' AND  EXAM.SNO='"+ sno +"' ";
            PreparedStatement pstm = conn.prepareStatement(sql);
            ResultSet rs = pstm.executeQuery();
            int count = 0;
            while (rs.next()) {
                count++;
            }
            rs = pstm.executeQuery();

            Object[][] data = new Object[count][7];
            String[] title = {"课程名","成绩","学期"};
            count = 0;
            while (rs.next()) {
                data[count][0] = (rs.getString("CNAME"));
                data[count][1] = (rs.getString("GRADE"));
                data[count][2] = (rs.getString("TERM"));
                count++;
            }
            JTable table = new JTable(data, title);
            JScrollPane scrollPane = new JScrollPane(table);
            DefaultTableCellRenderer cr = new DefaultTableCellRenderer();
            cr.setHorizontalAlignment(SwingConstants.CENTER);
            table.setDefaultRenderer(Object.class,cr);

            table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
            table.getColumnModel().getColumn(0).setPreferredWidth(200);
            table.getColumnModel().getColumn(1).setPreferredWidth(200);
            table.getColumnModel().getColumn(2).setPreferredWidth(200);
            table.setRowHeight(60);
            table.setFont(new Font("微软雅黑",Font.BOLD,25));
            scrollPane.setFont(new Font("宋体", Font.PLAIN, 20));
            scrollPane.setBounds(70, 120, 650, 300);
            student.add(scrollPane);

        }
        catch (ClassNotFoundException cnfe) {
            cnfe.printStackTrace();
            JOptionPane.showMessageDialog(null, "数据源错误", "错误", JOptionPane.ERROR_MESSAGE);
        }catch (
                SQLException sqle) {
            sqle.printStackTrace();
            JOptionPane.showMessageDialog(null, "数据操作错误", "错误", JOptionPane.ERROR_MESSAGE);
        }
        student.setVisible(true);
    }
}
